﻿
using System.Net;
using DABLL;
using DADAL;
using Microsoft.IdentityModel.Tokens;

namespace DataAcquisition
{
    public partial class HIDevicePointAddForm : BaseForm
    {
        #region 属性

        /// <summary>
        /// key
        /// </summary>
        private string EncryptionKey = SqlSugarHelper.EncryptionKey;
        
        /// <summary>
        /// 设备信息
        /// </summary>
        public DeviceModel? DeviceModelService;

        /// <summary>
        /// 类型
        /// </summary>
        public string PointType = string.Empty;

        #endregion

        #region 自定义方法
        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            PointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();

        }
        #endregion

        #region 系统方法
        public HIDevicePointAddForm()
        {
            InitializeComponent();
            Header.Text = "设备点位添加";
        }
        private void HIDevicePointAddForm_Load(object sender, EventArgs e)
        {
            if (DeviceModelService is not null)
            {
                DeviceNameInput.Text = DeviceModelService.Name;
                DeviceNumberInput.Text = DeviceModelService.Number.ToString();
                try
                {
                    DeviceAddressInput.Text = Encryption.DesDecrypt(DeviceModelService.SlaveAddress, EncryptionKey);
                }
                catch (Exception)
                {
                    
                }
                RegisterTypeSelect.Text = PointType;
                Reload();
            }
            else
            {
                this.Close();
                if (this.Owner is not null)
                {
                    DevicePointForm frm = (DevicePointForm)this.Owner;
                    frm.Error("未查询到设备的信息");
                    frm.LoadList();
                }
            }
        }

        #endregion

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            DevicePointModel model = new DevicePointModel();
            try
            {
                model.Number = Convert.ToInt32(PointNumberInput.Text);
            }
            catch (Exception)
            {
                Reload();
                Error("点位编号错误，请重新添加");
                return;
            }

            if (PointNameInput.Text.IsNullOrEmpty())
            {
                Error("请输入点位名称");
                return;
            }
            model.Name = PointNameInput.Text;

            if (PointAddressInput.Text.IsNullOrEmpty())
            {
                Error("请输入点位地址");
                return;
            }
            model.Address = PointAddressInput.Text;

            if (RegisterTypeSelect.Text.IsNullOrEmpty())
            {
                Error("请选择点位类型");
                return;
            }
            model.PointType = RegisterTypeSelect.Text;

            try
            {
                model.RangeMax = Convert.ToDouble(RangeMaxInput.Text);
            }
            catch (Exception)
            {
                model.RangeMax = 0;
            }
            try
            {
                model.OutMin = Convert.ToDouble(OutMinInput.Text);
            }
            catch (Exception)
            {
                model.OutMin = 0;
            }
            try
            {
                model.OutMax = Convert.ToDouble(OutMaxInput.Text);
            }
            catch (Exception)
            {
                model.OutMax = 0;
            }
            try
            {
                model.AlarmMin = Convert.ToDouble(AlarmLowInput.Text);
            }
            catch (Exception)
            {
                model.AlarmMin = 0;
            }
            try
            {
                model.AlarmMax = Convert.ToDouble(AlarmHightInput.Text);
            }
            catch (Exception)
            {
                model.AlarmMax = 0;
            }
            try
            {
                model.ErrorRange = Convert.ToDouble(ErrorRangeInput.Text);
            }
            catch (Exception)
            {
                model.ErrorRange = 0;
            }
            try
            {
                model.Amend = Convert.ToDouble(AmendInput.Text);
            }
            catch (Exception)
            {
                model.Amend = 0;
            }
            model.Unit = UnitInput.Text;

            if (DeviceModelService is not null)
            {
                model.DeviceNumber = DeviceModelService.Number;
                model.DeviceName = DeviceModelService.Name;
                model.DeviceAddress = DeviceModelService.SlaveAddress;
            }
            Response response = DevicePoint.AddDevicePoint(model);
            if (response.Code != 200)
            {
                Error(response.Msg);
                return;
            }
            else
            {
                Reload();
                this.Close();
                if (this.Owner is not null)
                {
                    DevicePointForm frm = (DevicePointForm)this.Owner;
                    frm.LoadList();
                }
            }
        }
    }
}
